import {ref} from 'vue'
import { defineStore } from 'pinia'

export interface Userinfo {
    avatar:string,
    nickname:string,
    organizationId:number,
    type:number,
    userId:string,
    username:string
}


export const useUserinfo = defineStore('userInfo', () => {
    const cacheLoginInfo=localStorage.getItem('login')
    let userInfo
    let userScope
    let roleCodes
    if(cacheLoginInfo){
        const {info,scopes,attributes}=JSON.parse(cacheLoginInfo)
        userInfo=info
        userScope=scopes
        roleCodes=attributes?.roleCodes
    }
    const user=ref(userInfo)
    const scope=ref<string[]>(userScope)
    const roles=ref<string[]>(roleCodes)
    const setUser=({info,scopes,attributes}:{info:Userinfo,scopes:string[],attributes:{roleCodes:string[]}})=>{
        user.value=info
        scope.value=scopes
        roles.value=attributes?.roleCodes
    }
    return {user,setUser,scope,roles}
})

